﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI.WebControls;

namespace CrazyTalk.Common
{

    /// <summary>
    /// 枚舉相關幫助類
    /// </summary>
    public class EnumHelper
    {
        /// <summary>
        /// 将枚举转换成List
        /// </summary>
        /// <param name="enumType">枚举的类型</param>
        /// <param name="allAllOption">if set to <c>true</c> [all all option].</param>
        /// <param name="isgetName">if set to <c>true</c> [isget name].</param>
        /// <returns></returns>
        public static List<ListItem> GetEnumList(Type enumType, bool allAllOption, bool isgetName)
        {
            if (enumType.IsEnum == false)
            {
                return null;
            }
            List<ListItem> list = new List<ListItem>();
            if (allAllOption == true)
            {
                list.Add(new ListItem("所有", "-1"));
            }

            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            string strValue = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.IsSpecialName) continue;
                //strValue = field.GetRawConstantValue().ToString();
                if (isgetName)
                {
                    strValue = field.Name.ToString();
                }
                else
                {
                    if (enumType.Name == "EGameStar")
                    {
                        strValue = decimal.Parse(field.GetRawConstantValue().ToString()).ToString("N1");
                    }
                    else
                    {
                        strValue = field.GetRawConstantValue().ToString();
                    }
                }
                object[] arr = field.GetCustomAttributes(typeDescription, true);
                if (arr.Length > 0)
                {
                    strText = (arr[0] as DescriptionAttribute).Description;
                }
                else
                {
                    strText = field.Name;
                }

                list.Add(new ListItem(strText, strValue));
            }
            return list;
        }

        /// <summary>
        /// Gets the enum list byvalue.
        /// </summary>
        /// <param name="enumType">Type of the enum.</param>
        /// <param name="allAllOption">if set to <c>true</c> [all all option].</param>
        /// <param name="isgetName">if set to <c>true</c> [isget name].</param>
        /// <param name="arrvalue">The arrvalue.</param>
        /// <returns></returns>
        public static List<ListItem> GetEnumListByvalue(Type enumType, bool allAllOption, bool isgetName, string[] arrvalue)
        {
            if (enumType.IsEnum == false)
            {
                return null;
            }
            List<ListItem> list = new List<ListItem>();
            if (allAllOption == true)
            {
                list.Add(new ListItem("所有", "-1"));
            }

            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            string strValue = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.IsSpecialName) continue;
                //strValue = field.GetRawConstantValue().ToString();
                if (isgetName)
                {
                    strValue = field.Name.ToString();
                }
                else
                {
                    strValue = field.GetRawConstantValue().ToString();
                }
                object[] arr = field.GetCustomAttributes(typeDescription, true);
                if (arr.Length > 0)
                {
                    strText = (arr[0] as DescriptionAttribute).Description;
                }
                else
                {
                    strText = field.Name;
                }
               
                if (arrvalue.Contains(strValue))
                {
                    list.Add(new ListItem(strText, strValue));
                }
            }
            return list;
        }

        /// <summary>
        /// Gets the enum list bytext.
        /// </summary>
        /// <param name="enumType">Type of the enum.</param>
        /// <param name="allAllOption">if set to <c>true</c> [all all option].</param>
        /// <param name="isgetName">if set to <c>true</c> [isget name].</param>
        /// <param name="arrText">The arr text.</param>
        /// <returns></returns>
        public static List<ListItem> GetEnumListBytext(Type enumType, bool allAllOption, bool isgetName, string[] arrText)
        {
            if (enumType.IsEnum == false)
            {
                return null;
            }
            List<ListItem> list = new List<ListItem>();
            if (allAllOption == true)
            {
                list.Add(new ListItem("所有", "-1"));
            }

            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            string strValue = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.IsSpecialName) continue;
                //strValue = field.GetRawConstantValue().ToString();
                if (isgetName)
                {
                    strValue = field.Name.ToString();
                }
                else
                {
                    strValue = field.GetRawConstantValue().ToString();
                }
                object[] arr = field.GetCustomAttributes(typeDescription, true);
                if (arr.Length > 0)
                {
                    strText = (arr[0] as DescriptionAttribute).Description;
                }
                else
                {
                    strText = field.Name;
                }

                if (arrText.Contains(strText.ToLower()))
                {
                    list.Add(new ListItem(strText, strValue));
                }
            }
            return list;
        }
    }

}
